/**
 * auto fill the online step
 */

var jq = jQuery;

var allAppMos = {
	'act' 		: 'act',
	'apps'		: 'apps',
	'huodong'	: 'huodong',
	'platform'	: 'platform',
	'points'	: 'points',
	'static'	: 'static',
	'static-act': 'static-act',
	'php'		: 'php',
	
	'dashan'	: 'appdashan',
	'hoho'		: 'apphoho',
	'share'		: 'appshare',
	'test'		: 'apptest',
	'vote'		: 'appvote',
	'photopk'	: 'photopk',
	'impress'	: 'impress',
	'duiwei'	: 'duiwei',
	'gift'		: 'gift',
	'suliao'	: 'suliao'
};

var bigUiMods = {
    'html': 'html',
    'html-static': 'html-static'
};

var normalMods = {
    'friend': 'friend',
    'friend-static': 'friend-static',
    'album': 'album',
    'vphoto': 'vphoto',
    'beautify': 'beautify',
    'cmt': 'cmt',
    'common': 'common',
    'note': 'note',
    'spregist': 'spregist',
    'icp': 'icp',
    'ifrient': 'ifrient',
    'ialbum': 'ialbum',
    'iblog': 'iblog',
    'icommon': 'icommon',
    'msg': 'msg',
    'message': 'message',
    'other': 'other',
    'plugins': 'plugins',
    'base': 'base',
    'frd': 'frdui',
    'ihome-inc': 'ihome-inc',
    'mainpage': 'mainpage',
    'myspace': 'myspace',
    'newindex': 'newindex',
    'udi': 'udi',
    'superpage': 'superpage'
};

var deploy_title_tpl = '$1、上线$2';
var rollback_title_tpl = '$1、$2回滚方案';

/********************App*********************/
var app_deploy_tpl = [
	'1、备份静态文件：备份nginx/htdocs/static/apps/$2下的文件',
	'2、备份模板：备份apache/htdocs/templates/templates/apps/$2下的文件',
	'3、解压$2.tar.gz',
	'4、上线静态文件：将static/apps/$2拷贝到nginx/htdocs/static/apps/$2',
	'5、上线模板：将templates/apps/$2拷贝到apache/htdocs/templates/templates/apps/$2',
	''	
].join('\n');
var app_rollback_tpl = [
	'1、回滚静态文件：将nginx/htdocs/static/apps/$2下的文件回滚到备份的版本',
	'2、回滚模板：将apache/htdocs/templates/templates/apps/$2下的文件回滚到备份的版本',
	''	
].join('\n');

/********************Normal*********************/
var normal_deploy_tpl = [
	'1、备份静态文件：备份nginx/htdocs/static/$2下的文件',
	'2、备份模板：备份apache/htdocs/templates/templates/$2下的文件',
	'3、解压$2.tar.gz',
	'4、上线静态文件：将static/$2拷贝到nginx/htdocs/static/$2',
	'5、上线模板：将templates/$2拷贝到apache/htdocs/templates/templates/$2',
	''	
].join('\n');
var normal_rollback_tpl = [
	'1、回滚静态文件：将nginx/htdocs/static/$2下的文件回滚到备份的版本',
	'2、回滚模板：将apache/htdocs/templates/templates/$2下的文件回滚到备份的版本',
	''	
].join('\n');

/********************html*********************/
var ui_html_deploy_tpl = [
	'1、备份大UI模板',
	'2、上线大UI模板，重启appui',
	''
].join('\n');
var ui_html_rollback_tpl = [
	'1、将大UI回滚到备份的版本',
	''
].join('\n');

/********************html-static*********************/
var ui_static_deploy_tpl = [
	'1、备份apache/htdocs下的文件',
	'2、解压static-space.bz2，将解压出来的所有文件拷贝到apache/htdocs下',
	''
].join('\n');
var ui_static_rollback_tpl = [
	'1、将apache/htdocs下的文件回滚到备份的版本',
	''
].join('\n');


//简要处理，应该没人一下上10个模块吧
var BigDecimalLetters = ['一','二','三','四','五','六','七','八','九','十'];

var warning_msg = [];
var in_array = function(arr,item) {
	for(var i = 0,len = arr.length;i < len;i++) {
		if(arr[i] == item) return true;
	}
	return false;
}

var allSelectedMods;

var getSelectedMods = function(){
	var mods = jq("#ContentPart .module-selector>div>ul:eq(0)").find("em");
	allSelectedMods = [];
	jq.each(mods,function(i,item){
		allSelectedMods.push($(item).html());
	});
	return allSelectedMods;
};

//onlineLog_deploystepDesc
var deploystepDesc = function(){
	var $onlineLog_deploystepDesc = jq("#onlineLog_deploystepDesc");
	jq.each(getSelectedMods(),function(i,item){
		
	});
};

//onlineLog_deploystepContent
var deployAndRollbackStepContent = function(){
	
	var deployInfo = [];
	var rollbackInfo = [];
	
	jq.each(getSelectedMods(),function(i,item){
		item = jq.trim(item);
		
		deployInfo.push(deploy_title_tpl.replace(/\$1/,BigDecimalLetters[i]).replace(/\$2/,item));
		rollbackInfo.push(rollback_title_tpl.replace(/\$1/,BigDecimalLetters[i]).replace(/\$2/,item));
		
		//app
		if(!!allAppMos[item]) {
			deployInfo.push(app_deploy_tpl.replace(/\$2/g,allAppMos[item]));
			rollbackInfo.push(app_rollback_tpl.replace(/\$2/g,allAppMos[item]));
		}
		//big ui (html,html-static)
		else if(!!bigUiMods[item]) {
			if(item == 'html') {
				deployInfo.push(ui_html_deploy_tpl);
				rollbackInfo.push(ui_html_rollback_tpl);
			} else {
				deployInfo.push(ui_static_deploy_tpl);
				rollbackInfo.push(ui_static_rollback_tpl);
			}
		}
		//normal 
		else {
			deployInfo.push(normal_deploy_tpl.replace(/\$2/g,normalMods[item] || item));
			rollbackInfo.push(normal_rollback_tpl.replace(/\$2/g,normalMods[item] || item));
			
			if(!normalMods[item] && !in_array(warning_msg,item)) {
				warning_msg.push(item);
			}
		}
	});
	
	jq("#onlineLog_deploystepContent").text(deployInfo.join("\n"));
	jq("#onlineLog_deploystepRollback").text(rollbackInfo.join('\n'));
};


chrome.extension.sendRequest({type:0});

chrome.extension.onRequest.addListener(function(req,sender){
	if(req.type != 1) return;
	if(jq("#ContentPart")[0]) {
		if(getSelectedMods().length == 0) {
			alert("^_^ 请先选择需要上线的模块，谢谢！");
			return;
		}
		warning_msg = [];
		deployAndRollbackStepContent();

		if(warning_msg.length) {
			alert("请确保如下模块是fe所属的前端模块：\n" + warning_msg.join(' , '));
		}
	}
});
